package system

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"jgyApi/server/global"
	"jgyApi/server/model/common/response"
	"jgyApi/server/model/system"
	systemReq "jgyApi/server/model/system/request"
	"jgyApi/server/utils"
)

type BrowserApi struct{}

func (b *BrowserApi) Index(c *gin.Context) {
	var ks systemReq.KeySn
	err := c.ShouldBind(&ks)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(ks, utils.KeySnVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	//校验key
	if !utils.IsThrough(ks.Key) {
		response.FailWithMessage("key不对", c)
		return
	}

	var rs utils.CacheResult
	rs = utils.GetRedisCacheField("sn", "value", ks.Sn, "batch")
	if rs.Data == nil || rs.Data == "" {
		response.FailWithMessage("该设备没有设置批次", c)
		return
	}
	type browser struct {
		ID         int64   `gorm:"column:id;type:int(10) unsigned;primaryKey;autoIncrement:true" json:"id"`
		BlackAddr *string `gorm:"column:black_addr;type:varchar(3000);comment:白网址" json:"black"`
		WhiteAddr *string `gorm:"column:white_addr;type:varchar(3000);comment:黑网址" json:"white"`
		BlackIP   *string `gorm:"column:black_ip;type:varchar(3000);comment:黑名单ip" json:"black_ip"`
		WhiteIP   *string `gorm:"column:white_ip;type:varchar(3000);comment:白名单ip" json:"white_ip"`
	}
	var bs browser
	where := fmt.Sprintf("find_in_set(%s,batch)", rs.Data)
	err = global.GVA_DB.Model(&system.Browser{}).Where(where).First(&bs).Error
	if err != nil {
		response.FailWithMessage("没有数据", c)
		return
	}
	response.OkWithDetailed(bs, "成功", c)
	return
}
